$(document).ready(function(e) {
    /*$(window).resize(function(){
		var w = $('.container').innerWidth();
		console.log("container: ",w);
		var body_w = $('body').innerWidth();
		console.log("body: ", body_w);
	});	*/	
	var count = 0;
	$('.seleComm select').change(		
		function(){
			var strVal = "";
			$(this).find('option:selected').each(function(){
				strVal = $(this).text();	
			});			
			$(this).parent('.seleComm').find('.listitem a').text(strVal);
		}
	);
	$('.ico_nav').click(
		function(){
			$("#map_view, #map_view.map_full").css("z-index","7");
			$(this).next('.subMenu').slideToggle();
		}
	);
	$('.ico_ope_map').click(function(){		
		$("#map_view").toggleClass("map_full");		
		$(this).toggleClass('ico_clo_map');		
		count++;	
		if(count%2 == 1){
			$("#map_view, #map_view.map_full").css("z-index","20");
			$('#uni_nav .subMenu').css("display","none");
			var h_window = $(window).height();		
			$('html, body').animate({
				scrollTop: $("#map_view").offset().top
			},400, 
				function(){					
					$('.boxMap').children('iframe').animate({height: h_window-10},800)
				}
			);
		}
		else{			
			$('html, body').animate({
				scrollTop: 0
			},400, 
				function(){
					$('.boxMap').children('iframe').animate({height: 300},800);
				}		
			);	
		}
	});
	
	$('.box_acc_setting > a').click(function(){
		$(this).next('.accUser_seting').slideToggle();
	});
		
	$(window).scroll(function() {
		if($(window).scrollTop() != 0) {
			$('#top_page').fadeIn(400);					
		}
		else{
			$('#top_page').fadeOut(200);
		}
	});
	$('#top_page').click(function() {
		$('html, body').animate({scrollTop:0},400);
	});
	$('.popup_fancybox').fancybox({
		maxWidth		: 510,
		minHeight		: 420,
		autoDimensions	: true,
		height			: 'auto',
		onUpdate    : function(){
			$.fancybox.update();
		}
		/*openEffect : 'elastic',
		openSpeed  : 150,
		closeEffect : 'elastic',
		closeSpeed  : 150,
		autoDimensions: true,
		height: 'auto',
		afterShow: function(){
			$.fancybox.update()
		}*/
	});
	$('.popup_fancybox_login').fancybox({
		maxWidth		: 350,
		minHeight		: 250,
		autoDimensions	: true,	
		height			: 'auto',	
		onUpdate    : function(){
			$.fancybox.update();
		}				
	});
});

App = {
	defaults : {
		
	},
	like : function(id){
		$.ajax({
			type: "POST",
			url: "/ajax/like",
			data: {id: id}
		}).done(function( data ) {
			if(data == "-1"){
				App.showLoginBox();	
			}
			else{
				alert('thanks');	
			}
		});
	},
	addSubscriber : function(email){
		if(email == '') alert('Vui lòng nhập email của bạn');
		else if(!Utils.isEmail(email)) alert('Email không hợp lệ');
		else {
			$.ajax({
				type: "POST",
				url: "/ajax/subscriber",
				data: { subscriber: email }
			}).done(function( msg ) {
				alert('Đăng ký email thành công, vui lòng vào hộp thư kích hoạt');
				$('#subscriber').val('');
			});	
		}
	},
	addComment : function(){
		
	},
	loadAds : function(zoneId){
		$.ajax({
			type: "POST",
			url: "/ajax/banners",
			data: { zone: zoneId }
		}).done(function( msg ) {
			/*var zone = document.getElementById('adv-'+zoneId);
			zone.innerHTML = msg;*/
			$('#adv-'+zoneId).html(msg);
		});
	},
	showLoginBox : function(){
		$.fancybox({
			type: 'ajax',
			href: '/auth/login'
        });
	},
	showRegisterBox : function(){
		$.fancybox({
			type: 'ajax',
			href: '/auth/register'
        });	
	},
	showUpload: function(){
		$.fancybox({
			type: 'ajax',
			href: '/deals/upload.html'
        });
	}
}

$.validator.addMethod("noSpace", function(value, element) {
	return (value.indexOf(" ") == -1) || (value.indexOf(" ") < 0 && value != ""); 
}, "this field requires no space");